package com.kll.tv.views

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.kll.carolinehmibiz.OperationManager
import com.kll.carolinehmibiz.model.AccountModel
import com.kll.carolinehmibiz.model.OperateListener
import com.kll.carolinehmibiz.net.bean.GetCalllogResponse
import com.kll.carolinehmibiz.net.bean.RefreshTokenResponse
import com.kll.carolinehmibiz.presentation.listener.MyAppointmentProtocol
import com.kll.carolinehmibiz.presentation.presenter.MyAppointmentPresenter
import com.kll.tv.base.views.BaseActivity
import com.kll.tv.databinding.ActivityMyAppointmentBinding
import com.kll.tv.utils.AppToast
import com.kll.tv.views.adapter.MyAppointmentAdapter

/**
 * 我的预约页
 */
class MyAppointmentActivity: BaseActivity<MyAppointmentPresenter>(), MyAppointmentProtocol.IView,
MyAppointmentAdapter.OnAppointmentListItemClickListener{
    private lateinit var binding: ActivityMyAppointmentBinding
    private var adapter: MyAppointmentAdapter? = null

    override fun initViewBinding(): View {
        binding = ActivityMyAppointmentBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun createPresenter(): MyAppointmentPresenter {
        return MyAppointmentPresenter(this, this)
    }

    override fun init() {
        // 初始化adapter
        if (adapter == null) {
            val appointmentManager = LinearLayoutManager(this)
            appointmentManager.orientation = LinearLayoutManager.VERTICAL
            binding.rvContent.layoutManager = appointmentManager
            adapter = MyAppointmentAdapter(this)
            binding.rvContent.adapter = adapter
            adapter!!.setOnAppointmentListItemClickListener(this)
        }

        initData()
        initListener()
    }

    private fun initData() {
        mPresenter.getCalllog(true)
    }

    private fun initListener() {
        binding.ivBack.setOnClickListener {
            finish()
        }
    }

    override fun getCalllogSuccessed(data: ArrayList<GetCalllogResponse.CallLog>) {
        adapter!!.data = data
    }

    /**
     * 获取我的预约失败
     */
    override fun getCalllogFailed(code: Int, message: String) {
        AppToast.show(message, 2000)
        if (code == 102) {
            // token过期 刷新token
            OperationManager.getInstancce().refreshToken(object : OperateListener {
                override fun onSuccess(state: Int, obj: Any?) {
                    var data = obj as RefreshTokenResponse
                    AccountModel.getInstance(baseContext).updateUserToken(data.token)
                    mPresenter.getCalllog(true)
                }

                override fun onFail(code: Int, message: String) {
                    AppToast.show(message, 2000)
                    if (code == 101) {
                        loginAgain()
                    }
                }

            })
        }
        if (code == 101) {
            loginAgain()
        }
    }

    /**
     * item点击回调
     */
    override fun onAppointmentItemClick(data: GetCalllogResponse.CallLog) {
        AppToast.show("${data.staff_name}", 2000)
    }
}